#!/bin/sh

export LD_LIBRARY_PATH="/mnt/ESensor/lib:$LD_LIBRARY_PATH"

PRJ_PATH=$1
NETCONFPATH="/tmp/etc/network_config"
ETHERNETCONFPATH="/tmp/etc/ethernet_config"
MOBILENETCONFPATH="/tmp/etc/mobilenet_config"

if [ -d $PRJ_PATH"/ethernet" ]; then ln -s $PRJ_PATH/ethernet 	/usr/ethernet; fi
if [ -d $PRJ_PATH"/mobilenet" ]; then ln -s $PRJ_PATH/mobilenet	/usr/mobilenet; fi
if [ -d $PRJ_PATH"/wifi" ]; 	then ln -s $PRJ_PATH/wifi 	/usr/wifi; fi
if [ -d $PRJ_PATH"/netplug" ]; then ln -s $PRJ_PATH/netplug   /usr/netplug; fi

# Open loopback interface for flv streamer to RTMP server
ifconfig lo up
ret=1

# Ethernet/PPPoE
if [ -d "/usr/ethernet" ]; then
        cd      /usr/ethernet
        if [ ! -f $ETHERNETCONFPATH ]; then
             if [ -f "/usr/ethernet/ethernet_config.default" ]; then
                     echo "Using default network configuration file." > $MSGTOTERMINAL
                     cp -a /usr/ethernet/ethernet_config.default     $ETHERNETCONFPATH
             fi
        fi
	./ethernet.sh
	ret=`expr $ret \* $?`
fi

# MobileNet 3G/4G
if [ -d "/usr/mobilenet" ]; then
        cd      /usr/mobilenet
        if [ ! -f $MOBILENETCONFPATH ]; then
             if [ -f "/usr/mobilenet/mobilenet_config.default" ]; then
                     echo "Using default network configuration file." > $MSGTOTERMINAL
                     cp -a /usr/mobilenet/mobilenet_config.default     $MOBILENETCONFPATH
             fi
        fi
	./mobilenet.sh start
	ret=`expr $ret \* $?`
fi

# Wi-Fi setting
if [ -d "/usr/wifi" ]; then
    cd      /usr/wifi
    if [ ! -f $NETCONFPATH ]; then
	     if [ -f "/usr/wifi/network_config.default" ]; then
	             echo "Using default network configuration file." > $MSGTOTERMINAL
                     cp -a /usr/wifi/network_config.default 	$NETCONFPATH
		     sync
             fi
    fi


	if [ -f $NETCONFPATH ]; then

#               if kpdin -t 0; then
#		        echo "Enable QR viewer? Press <" $WPS_TRIG_KEY_STR "> key" > $MSGTOTERMINAL
#		        kpdin -t 2
#			ret=$?
#		else
#			ret=0
#		endif
#	        if [ $ret -gt 0 ]; then
#	             echo "Starting QR view." > $MSGTOTERMINAL
#	                /mnt/ESensor/bin/qrcam.sh
#	                if [ $? != 0 ]; then    exit 1; fi
#	                cp -a /tmp/zbar         $NETCONFPATH
#	                rm -f /tmp/etc/wpa.conf
#	                rm -f /tmp/etc/hostapd.conf
#	                sync
#	                echo "Save network configuration [OK]." > $MSGTOTERMINAL
#	        fi

                if kpdin -h > /dev/null; then
	                echo "Enable WPS? <Press any key>" > $MSGTOTERMINAL
                        kpdin -t 2
                        ret=$?
                else
                        ret=0
                fi
        	if [ $ret -gt 0 ]; then
                	# WPS
                	echo "Starting WPS PBC mode." > $MSGTOTERMINAL
	                echo "Please press WPS button on AP" > $MSGTOTERMINAL
			if ./network.sh WPS PBC; then	
				# Todo check status at here
				echo "WPS PBC mode [OK]." > $MSGTOTERMINAL
			fi
	        else
	                echo "Configure network setting ..." > $MSGTOTERMINAL		        
                        if ./network.sh; then
				echo "Configure network successfully!!" > $MSGTOTERMINAL
				ret=0
			fi
	        fi
	fi
fi

#==============================================================================
#	Start Netplug or not
#==============================================================================
if [ -d "/usr/netplug" ]; then
        cd /usr/netplug
        ./netplugd.sh &
fi


echo "Net Config Result $?"

#有线网卡检测
ETHERNET_FILE=/tmp/etc/ethernet_config
if [ ! -f $ETHERNET_FILE ]; then
	echo "Can't find $ETHERNET_FILE"
else
	# set BOOTPROTO to DHCP/STATIC
	NET_BOOTPROTO=`awk '{if ($1=="BOOTPROTO") {print $2}}' $ETHERNET_FILE`
	cd /mnt/nand1-1
	./netlink
	if [ $? -eq 0 ]; then
		echo "none net"
	else
		echo "have net"
		if [ "$NET_BOOTPROTO" == "DHCP" ]; then
		udhcpc -i eth0 -T 15 -A 1 -q -n
	fi
fi

ping -c3 www.baidu.com     

cd /mnt/nand1-2
#启动Telnet服务
if [ -d "/mnt/nand1-1/netplugx" ]; then
echo "start telnetd server"
/mnt/nand1-1/netplugx/network/if-up.d/02telnetd 
fi

exit $ret
